package com.block.api.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.block.channel.service.IChannelInfoService;
import com.block.channel.service.ICountInfoService;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.exception.ApiException;
import com.block.common.model.dto.CommonReqDTO;
import com.block.common.model.dto.UserInfoDTO;
import com.block.common.security.util.ApiUtil;
import com.block.common.service.IIpInfoService;
import com.block.common.service.ISysConstantInfoService;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.sms.service.ISmsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("api/sys")
@Validated
public class SysController {

    @Autowired
    ISmsRecordService smsRecordService;
    @Autowired
    ICountInfoService countInfoService;
    @Autowired
    IChannelInfoService channelInfoService;
    @Autowired
    ISysConstantInfoService sysConstantInfoService;
    @Autowired
    IIpInfoService iIpInfoService;

    /**
     * 数据统计
     * api/sys/count
     * 根据operType 确定统计的类型
     */
    @NeedToken(need = false)
    @RequestMapping(value = "/count", method = {RequestMethod.POST, RequestMethod.GET})
    public String searchIcon(@RequestAttribute(value = "tranData") JSONObject tranData,
                                @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                @RequestAttribute(value = "uri") String uri,
                                @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                @RequestAttribute(value = "startDate") Date startdate,
                                @RequestAttribute(value = "ip") String ip) throws ApiException {

        String operType = tranData.getString("operType");// 类型(下载)(prod-uv)(resource)
        String dataId = tranData.getString("dataId");// 数据
        String dataInfo = tranData.getString("dataInfo");// 数据
        String dataExt = tranData.getString("dataExt");// 数据

        UserInfoDTO userInfoDTO = new UserInfoDTO();
        userInfoDTO.setUid(sessionBean.getUid());
        userInfoDTO.setChannelCode(sessionBean.getChannelCode());
        userInfoDTO.setChannelName(sessionBean.getChannelName());
        userInfoDTO.setReqChannelCode(commonParam.getChannelCode());
        userInfoDTO.setReqChannelName(commonParam.getChannelName());
        userInfoDTO.setClientType(commonParam.getClientType());
        userInfoDTO.setIp(ip);

        ResponseVo responseVo = countInfoService.handleOperData(operType, dataId, userInfoDTO, dataInfo, dataExt, commonParam);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 根据渠道号获取 对应的落地页的类型
     *
     */
    @RequestMapping(value = "/getPageType", method = {RequestMethod.POST, RequestMethod.GET})
    public String getPageType(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {


        CommonReqDTO commonReqDTO = new CommonReqDTO();
        commonReqDTO.setReqIp(ip);
        commonReqDTO.setReqChannelCode(commonParam.getChannelCode());
        commonReqDTO.setReqChannelName(commonParam.getChannelName());

        ResponseVo responseVo = channelInfoService.getPageTypeByCode(commonReqDTO);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 获取系统设置的一些常量信息
     */
    @RequestMapping(value = "/getAppConfigInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getAppConfigInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                              @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                              @RequestAttribute(value = "uri") String uri,
                              @RequestAttribute(value = "commonParam") CommonParam commonParam,
                              @RequestAttribute(value = "startDate") Date startdate,
                              @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = sysConstantInfoService.getAppConfigInfo();
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 获取系统设置的一些常量通知信息
     */
    @RequestMapping(value = "/noticeList", method = {RequestMethod.POST, RequestMethod.GET})
    public String noticeList(@RequestAttribute(value = "tranData") JSONObject tranData,
                                   @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                   @RequestAttribute(value = "uri") String uri,
                                   @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                   @RequestAttribute(value = "startDate") Date startdate,
                                   @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = sysConstantInfoService.getNoticeList();
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 获取ip信息
     * 判断ip 等信息
     */
    @RequestMapping(value = "/getipinfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getIpInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                          @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                          @RequestAttribute(value = "uri") String uri,
                          @RequestAttribute(value = "commonParam") CommonParam commonParam,
                          @RequestAttribute(value = "startDate") Date startdate,
                          @RequestAttribute(value = "ip") String ip,
                          HttpServletRequest request) throws ApiException {

        ResponseVo responseVo = iIpInfoService.getIpInfoResult(ip);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

}
